package day1206zy.day1206zy;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class demo5 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class<?> cat = Class.forName("day1206zy.Cat");
//        通过构造方法创建对象（可用链式编程）
        Constructor<?> c1 = cat.getDeclaredConstructor();
        Object o = c1.newInstance();
//        获取Filed对象
        Field f1 = cat.getDeclaredField("name");
//        设置该方法在外界暂时可用
        f1.setAccessible(true);
//        Object str = f1.get(o);
//        System.out.println(str);
//        取值赋值
        f1.set(o,"大黄");
        Object o1 = f1.get(o);
        System.out.println(o1);
    }
}
